/*
 * @Description: It was written by Pony˜
 * @Version: 2.0
 * @Autor: Pony
 * @Date: 2020-10-21 17:28:36
 * @LastEditors: zhang zhen
 * @LastEditTime: 2024-04-29 18:09:09
 */
const { app, BrowserWindow, Menu, Tray, nativeImage } = require('electron')
function createMenu () {
  const template = [
    {
      label: '菜单',
      submenu: [
        {
          label: '开启/退出全屏',
          click: () => toggleFullScreen()
        }
      ]
    },
  ];
  const menu = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(menu);
}
function createWindow () {
  const win = new BrowserWindow({
    width: 1920,
    height: 1080,
    webPreferences: {
      nodeIntegration: true
    }
  })
  win.loadURL('http://112.2.0.205:16202')
  // win.loadFile('index.html')
  // win.webContents.openDevTools()
  const contents = win.webContents
  // console.log(contents)
  win.setTitle('梅钢远程运维')

}
// 添加全屏切换功能  
function toggleFullScreen() {  
  if (BrowserWindow.getFocusedWindow()) {  
    const window = BrowserWindow.getFocusedWindow();  
    if (window.isFullScreen()) {  
      window.setFullScreen(false);  
    } else {  
      window.setFullScreen(true);  
    }  
  }  
} 
// 确保在 app ready 事件之后使用 Tray  
app.on('ready', () => {  
  // 使用 nativeImage 模块的 createFromPath 方法来加载图标  
  const iconPath = `./public/logo.ico`;  
  const icon = nativeImage.createFromPath(iconPath);  
  
  // 创建 Tray 实例  
  let tray = new Tray(icon);  
  
  // 创建上下文菜单  
  const contextMenu = Menu.buildFromTemplate([  
    {  
      label: '退出',  
      click: function() {  
        app.quit();  
      }  
    }  
  ]);  
  
  // 设置托盘图标的提示文本  
  tray.setToolTip('梅钢远程运维');  
  
  // 设置托盘图标的上下文菜单  
  tray.setContextMenu(contextMenu);  
});
createMenu()
app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow()
  }
})
